﻿using CSRedis;
using DotNetCommon;
using furion.blog.Application.Contracts;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Utils;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 图片操作类
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "ImageApi" })]
    [Route("api/image")]
    public class ImageApiController : IDynamicApiController
    {
        private readonly IImageService _imageService;
        private readonly CSRedisClient[] _csredis;

        public ImageApiController(CSRedisClient[] _csredis, IImageService imageService)
        {
            this._csredis = _csredis;
            this._imageService = imageService;
        }

        [ActionAttribute(Name = "验证码接口", Desc = "图片验证码")]
        [HttpGet, Route("captcha"), AllowAnonymous]
        public async Task<IActionResult> captcha()
        {
            var (code, base64) = VerifyCodeHelper.New();
            string token = IdUtil.Id().ToString();
            await _csredis[15].SetAsync(token, code, 300);
            return new OkObjectResult(ResultUtil.Success(new { base64 = "Data:image/jpg;base64," + base64, token = token }));
        }

        [ActionAttribute(Name = "头像上传接口", Desc = "会员上传头像")]
        [HttpPost, Route("avatar/upload")]
        public async Task<IActionResult> uploadAvatar([FromForm(Name = "file")] List<IFormFile> files)
        {
            var ret = await this._imageService.Save(files);
            return new OkObjectResult(ResultUtil.Success(ret));
        }
    }
}
